﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;

using System.ServiceModel.DomainServices.Client;

using System.Text;

namespace LEEAAttendanceControl.Views
{
    public partial class ExceptionAssignmentEdit : ChildWindow
    {
        LEEAContext context = new LEEAContext();

        ExceptionAssignment assignment = new ExceptionAssignment();

        ExceptionAssignmentSummary summary = null;

        int assignmentID = 0;

        public ExceptionAssignmentEdit()
        {
            InitializeComponent();
        }

        public void LoadData(int empID, int assID)
        {
            assignmentID = assID;

            context.Load(context.GetExceptionAssignmentSummaryByEmployeeIDQuery(empID), OnSummaryLoadCompleted, null);
        }

        private void OnSummaryLoadCompleted(LoadOperation load)
        {
            summary = load.Entities.FirstOrDefault() as ExceptionAssignmentSummary;

            NameBox.Text = summary.FirstName + " " + summary.LastName;

            DepartmentBox.Text = summary.Department;

            context.Load(context.GetExceptionAssignmentByIDQuery(assignmentID), OnAssignmentLoadCompleted, null);
        }

        private void OnAssignmentLoadCompleted(LoadOperation load)
        {
            assignment = (ExceptionAssignment)load.Entities.FirstOrDefault();

            FromPicker.SelectedDate = assignment.DateFrom.Value;

            DurationUD.Value = assignment.DateTo.Value.Subtract(assignment.DateFrom.Value).Days + 1;

            context.Load(context.GetExceptionByIdQuery(assignment.ExceptionID.Value)).Completed += (s, e) =>
                {
                    ExNameBox.Text = ((Exeption)context.Exeptions.FirstOrDefault()).Name;
                };
        }

        private void ExceptionsDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            assignment.DateFrom = FromPicker.SelectedDate;

            assignment.DateTo = FromPicker.SelectedDate.Value.AddDays(((int)DurationUD.Value) - 1);

            context.SubmitChanges(OnChangesSubmitted, null);
        }

        private void OnChangesSubmitted(SubmitOperation so)
        {
            if (so.HasError)
            {
                so.MarkErrorAsHandled();
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralError
                    , ApplicationStrings.GeneralError, "Error");
                message.Show();
            }
            else
            {
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralSave
                    , ApplicationStrings.GeneralSave, "Save");

                message.Closed += (s, e) =>
                    {
                        this.DialogResult = true;
                    };

                message.Show();
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

